package com.bp.hearthealth.pulse.lnapps.bp_ui.bp_dialog

import android.app.Activity
import android.app.Dialog
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import androidx.databinding.DataBindingUtil
import com.bp.hearthealth.pulse.lnapps.R
import com.bp.hearthealth.pulse.lnapps.bp_extension.LogUtil
import com.bp.hearthealth.pulse.lnapps.bp_extension.UIHelperBp
import com.bp.hearthealth.pulse.lnapps.bp_utils.DoubleClickListener
import com.bp.hearthealth.pulse.lnapps.bp_view.RatingBar
import com.bp.hearthealth.pulse.lnapps.databinding.EvaluateLayoutBinding
import com.hjq.toast.Toaster

class EvaluateDialog(context: Activity) {
    private lateinit var binding: EvaluateLayoutBinding
    private lateinit var onCliclLisener: OnBtnClickListener
    private lateinit var dialog: Dialog
    private val activity: Activity = context

    fun showDialog() {
        dialog = Dialog(activity, R.style.My_Dialog_FullScreen)
        binding = DataBindingUtil.inflate(
            LayoutInflater.from(activity), R.layout.evaluate_layout, null, false
        )
        binding.root.let { dialog.setContentView(it) }//核心代码
        dialog.setCanceledOnTouchOutside(false)
        dialog.setCancelable(false)
        val lp = dialog.window?.attributes
        lp?.width = WindowManager.LayoutParams.MATCH_PARENT
        lp?.height = WindowManager.LayoutParams.WRAP_CONTENT
        lp?.gravity = Gravity.BOTTOM
        lp?.alpha = 1f
        dialog.window?.attributes = lp
        if (activity.isFinishing) {
            return
        }
        dialog.show()
        binding.ivCancel.setOnClickListener(object : DoubleClickListener() {
            override fun onNoDoubleClick(v: View?) {
                dialog.dismiss()
            }
        })
        binding.tvSave.setOnClickListener(object : DoubleClickListener() {
            override fun onNoDoubleClick(v: View?) {
                dismiss()
//                if (binding.editEvaluate.text.toString().isEmpty()){
//                    Toaster.show("")
//                    return
//                }
                LogUtil.i("ratingCount ${binding.ratingCount.toString()}")
                if (binding.ratingCount!!.toFloat() < 1) {
                    Toaster.show("")
                    return
                }
                onCliclLisener.onBtnClick(binding.ratingCount!!.toInt())
            }
        })
        binding.ratingCount = 0f
        binding.ratingBar.isClickable = true //设置可否点击
        binding.ratingBar.setStepSize(RatingBar.StepSize.Full) //设置每次点击增加一颗星还是半颗星
        binding.ratingBar.setOnRatingChangeListener(object : RatingBar.OnRatingChangeListener {
            override fun onRatingChange(ratingCount: Float) {
                binding.ratingCount = ratingCount
                if (ratingCount > 3) {
                    binding.ivIcon.setImageResource(R.mipmap.happy_icon)
                    binding.tvSave.text = UIHelperBp.getString(R.string._comment_str)
                    binding.tvTitleDes.text = UIHelperBp.getString(R.string._evaluate_high_str)
                } else {
                    binding.ivIcon.setImageResource(R.mipmap.frustrated_icon)
                    binding.tvSave.text = UIHelperBp.getString(R.string._submit_str)
                    binding.tvTitleDes.text = UIHelperBp.getString(R.string._evaluate_low_str)
                }
            }
        })
    }

    fun dismiss() {
        dialog.dismiss()
    }

    fun setOnBtnClickLisener(clickListener: OnBtnClickListener) {
        this.onCliclLisener = clickListener
    }

    interface OnBtnClickListener {
        fun onBtnClick(ratingCount: Int)
    }
}
